package com.oig.common.module;

import com.oig.common.constants.CommonConstants;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 利用 http status，其实不需要这个，
 * 当异常时返回非200，body放异常提示
 * 成功都返回 200
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Schema
public class SysResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    public static <T> SysResponse<T> ok() {
        return restResponse(null, CommonConstants.SUCCESS, null);
    }

    public static <T> SysResponse<T> ok(T data) {
        return restResponse(data, CommonConstants.SUCCESS, null);
    }

    public static <T> SysResponse<T> ok(T data, String msg) {
        return restResponse(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> SysResponse<T> failed() {
        return restResponse(null, CommonConstants.FAIL, null);
    }

    public static <T> SysResponse<T> failed(String msg) {
        return restResponse(null, CommonConstants.FAIL, msg);
    }

    public static <T> SysResponse<T> failed(T data) {
        return restResponse(data, CommonConstants.FAIL, null);
    }

    public static <T> SysResponse<T> failed(T data, String msg) {
        return restResponse(data, CommonConstants.FAIL, msg);
    }

    private static <T> SysResponse<T> restResponse(T data, int code, String msg) {
        SysResponse<T> response = new SysResponse<>();
        response.setCode(code);
        response.setData(data);
        response.setMsg(msg);
        return response;
    }


}
